<?php
// +----------------------------------------------------------------------
// |采购管理控制器
// +----------------------------------------------------------------------


namespace app\admin\controller;


use app\BaseController;
use think\facade\View;
use app\admin\system\PurchaseService;
use app\admin\Model\SupplierModel;
use app\common\Model\UserModel;
use app\admin\Model\PurchaseModel;
use app\common\Model\StroageModel;


class Purchase extends BaseController
{
     protected $Model,$Stroage;
     // 构造函数
    public function __construct()
    {
        $this->Stroage = new StroageModel();
        $this->Model = new PurchaseModel();
    }   

    // 采购列表
    public function index(){
         //查询仓库         
         $storagelist =   $this->Stroage->getActivelist();      
        if(request()->isPost()){
           $param = request()->post();
           $data=PurchaseService::index($param);
           success($data);
        }else{
            //获取支付状态
            $paid_status =  $this->Model->getpaid_status();
            view::assign('paid_status',json_encode($paid_status));
            //采购单状态
            $status =  $this->Model->getstatus();
            view::assign('status',json_encode($status));
            //仓库
            view::assign('storagelist',json_encode($storagelist));
            return View::fetch();
        }
    }
    //采购单取消
    public function cancel_purchase(){
        if(request()->isPost()){
           $param = request()->post();
           $data=PurchaseService::cancel_purchase($param);
           success($data);
        }
    }
    //采购单详情
    public function purchase_detail(){
        if(request()->isPost()){
                $param = request()->post();    
                $data= PurchaseService::purchase_detail($param);         
                success($data);
        }else{
            $param = request()->get();       
            View::assign('id',$param['id']); 
            return view::fetch();
        } 
    }     

   // 待采购列表
   public function procurement(){
        if(request()->isPost()){
           $param = request()->post();
           $data=PurchaseService::getList($param);
           success($data);
        }else{
            view::assign('supplier_list',json_encode((new SupplierModel)->getActivelist()));
            return View::fetch();
        }
   }
   //待采购生成采购单
   public function create_purchase(){
        if(request()->isPost()){
            $param = request()->post();
            $data=PurchaseService::create_purchase($param);
            success($data);
        }
    }
    //取消待采购单详情单
    public function cancel_purchase_itmes(){
        if(request()->isPost()){
            $param = request()->post();
            $data=PurchaseService::cancel_purchase_itmes($param);
            success($data);
        }
    }
    //待采购详情单列表
    public function  procurement_itmes(){
        if(request()->isPost()){
            $param = request()->post();
            $data=PurchaseService::procurement_itmes($param);
            success($data);
        }else{
            $param = request()->get(); 
            view::assign('user_list',json_encode((new UserModel)->getActivelist()));
            view::assign('supplier_list',json_encode((new SupplierModel)->getActivelist()));             
            View::assign('id',$param['id']); 
            return View::fetch();
        }
    }
     //修改待采购详情单采购数量列表
    public function purchase_save_num(){
        if(request()->isPost()){
            $param = request()->post();
            $data=PurchaseService::purchase_save_num($param);
            success($data);
        }
    }

    //添加采购单到待采购
    public function addpurchase(){
        if(request()->isPost()){
            $param = request()->post();
            $data=PurchaseService::addpurchase($param);
            success($data);
       }else{
            $buyerinfo= (new UserModel())->getuserlist(3);  //获取采购员  
             view::assign('buyerinfo',json_encode($buyerinfo)); 
            //查询仓库         
            $storagelist =   $this->Stroage->getActivelist();      
              //仓库
            view::assign('storagelist',json_encode($storagelist));         
            $userinfo= (new UserModel())->getuserlist(4);  //获取业务员 
             view::assign('userinfo',json_encode($userinfo));       
            return View::fetch();
       }
    }

    //批量导入添加采购单到待采购
    public function import_purchase(){
        if(request()->isPost()){
            $param = request()->post();
            $data=PurchaseService::import_purchase($param);
            success($data);
       }else{  
            return View::fetch();
       }       
    }      

}